<?php

class Xzsdsplus_Costpriceplus_Model_Resource_Mysql4_Rewrite_Bundle_Indexer_Price
    extends Mage_Bundle_Model_Mysql4_Indexer_Price
{

    protected function _prepareBundlePriceByType($priceType, $entityIds = null)
    {
        $write = $this->_getWriteAdapter();
        $table = $this->_getBundlePriceTable();

        $select = $write->select()
            ->from(array('e' => $this->getTable('catalog/product')), array('entity_id'))
            ->join(
                array('cg' => $this->getTable('customer/customer_group')),
                '',
                array('customer_group_id'));
        $this->_addWebsiteJoinToSelect($select, true);
        $this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', 'e.entity_id');
        $select->columns('website_id', 'cw')
            ->join(
                array('cwd' => $this->_getWebsiteDateTable()),
                'cw.website_id = cwd.website_id',
                array())
            ->joinLeft(
                array('tp' => $this->_getTierPriceIndexTable()),
                'tp.entity_id = e.entity_id AND tp.website_id = cw.website_id'
                    . ' AND tp.customer_group_id = cg.customer_group_id',
                array())
            ->where('e.type_id=?', $this->getTypeId());

        // add enable products limitation
        $statusCond = $write->quoteInto('=?', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
        $this->_addAttributeToSelect($select, 'status', 'e.entity_id', 'cs.store_id', $statusCond, true);

        $taxClassId = $this->_addAttributeToSelect($select, 'tax_class_id', 'e.entity_id', 'cs.store_id');
        $select->columns(array('tax_class_id' => new Zend_Db_Expr("IF($taxClassId IS NOT NULL, $taxClassId, 0)")));

        $priceTypeCond = $write->quoteInto('=?', $priceType);
        $this->_addAttributeToSelect($select, 'price_type', 'e.entity_id', 'cs.store_id', $priceTypeCond);

        $cppGroup       = $this->_addAttributeToSelect($select, 'cpp_group', 'e.entity_id', 'cs.store_id'); 
        
        $select->joinLeft(
            array('cppg' => $this->getTable('costpriceplus/groups')),
            "cppg.entity_id = {$cppGroup}",
            array() );
        
        $select->joinLeft(
            array('cppp' => $this->getTable('costpriceplus/product_price_index')),
            "cppp.product_id = e.entity_id AND cppp.website_id = cw.website_id",
            array() );
         
        $price              = $this->_addAttributeToSelect($select, 'price', 'e.entity_id', 'cs.store_id');
        
        $newPrice           = new Zend_Db_Expr("IF((IFNULL({$cppGroup}, 0) <> 0) AND (cppg.is_active = 1) AND (cppp.price IS NOT NULL), cppp.price, {$price})");                                            

        $specialPrice   = $this->_addAttributeToSelect($select, 'special_price', 'e.entity_id', 'cs.store_id');
        $specialFrom    = $this->_addAttributeToSelect($select, 'special_from_date', 'e.entity_id', 'cs.store_id');
        $specialTo      = $this->_addAttributeToSelect($select, 'special_to_date', 'e.entity_id', 'cs.store_id');
        $curentDate     = new Zend_Db_Expr('cwd.date');

        $specialExpr    = new Zend_Db_Expr("IF(IF({$specialFrom} IS NULL, 1, "
            . "IF({$specialFrom} <= {$curentDate}, 1, 0)) > 0 AND IF({$specialTo} IS NULL, 1, "
            . "IF({$specialTo} >= {$curentDate}, 1, 0)) > 0 AND {$specialPrice} > 0, $specialPrice, 0)");
        $tierExpr       = new Zend_Db_Expr("tp.min_price");

        if ($priceType == Mage_Bundle_Model_Product_Price::PRICE_TYPE_FIXED) {
            $finalPrice  = new Zend_Db_Expr("IF({$specialExpr} > 0,"
                . " ROUND({$newPrice} * ({$specialExpr} / 100), 4), {$newPrice})");
            $tierPrice      = new Zend_Db_Expr("IF({$tierExpr} IS NOT NULL,"
                . " ROUND({$newPrice} - ({$newPrice} * ({$tierExpr} / 100)), 4), NULL)");
        } else {
            $finalPrice     = new Zend_Db_Expr("0");
            $tierPrice      = new Zend_Db_Expr("IF({$tierExpr} IS NOT NULL, 0, NULL)");
        }

        $select->columns(array(
            'price_type'    => new Zend_Db_Expr($priceType),
            'special_price' => $specialExpr,
            'tier_percent'  => $tierExpr,
            'orig_price'    => new Zend_Db_Expr("IF({$price} IS NULL, 0, {$price})"),
            'price'         => $finalPrice,
            'min_price'     => $finalPrice,
            'max_price'     => $finalPrice,
            'tier_price'    => $tierPrice,
            'base_tier'     => $tierPrice,
        ));

        if (!is_null($entityIds)) {
            $select->where('e.entity_id IN(?)', $entityIds);
        }
        
        /**
         * Add additional external limitation
         */
        Mage::dispatchEvent('catalog_product_prepare_index_select', array(
            'select'        => $select,
            'entity_field'  => new Zend_Db_Expr('e.entity_id'),
            'website_field' => new Zend_Db_Expr('cw.website_id'),
            'store_field'   => new Zend_Db_Expr('cs.store_id')
        ));

        $query = $select->insertFromSelect($table);
        $write->query($query);

        return $this;
    }
    
    protected function _addConfigDataToSelect($select, $path, $website, $store)
    {
        $select
            ->joinLeft(
                array('default' => $this->getTable('core/config_data')),
                "default.path = '{$path}' AND default.scope = 'default'",
                array())
            ->joinLeft(
                array('websites' => $this->getTable('core/config_data')),
                "websites.path = '{$path}' AND websites.scope = 'websites' AND websites.scope_id = {$website}",
                array())
            ->joinLeft(
                array('stores' => $this->getTable('core/config_data')),
                "stores.path = '{$path}' AND stores.scope = 'stores' AND stores.scope_id = {$store}",
                array());
        return new Zend_Db_Expr("IFNULL(stores.value, IFNULL(websites.value, IFNULL(default.value, 1)))");
    }
} 